package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.dto.CommodityInfoDTO;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.order.models.dto.purchase.PurchaseCommodityBargainDTO;
import com.ztgf.order.models.service.purchase.IPurchaseCommodityBargainService;
import com.ztgf.shopping.mapper.shop.ShopGoodsCountMapper;
import com.ztgf.shopping.models.service.shop.IShopGoodsCountService;
import com.ztgf.shopping.models.vo.shop.ShopGoodsCountVO;
import com.ztgf.shopping.models.vo.shop.ShopGoodsSalesRankVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static com.ztgf.common.utils.UtilConstants.ONE;
import static com.ztgf.common.utils.UtilConstants.ZERO;

/*
 * @author: chenyu.wang
 * @date: 2020-08-19
 * @description:卖家店铺产品统计服务
 */
@DubboService
public class ShopGoodsCountServiceImpl implements IShopGoodsCountService {

    @DubboReference
    private ICommodityInfoService commodityInfoService;
    @Autowired
    private ShopGoodsCountMapper shopGoodsCountMapper;


    /*
     * @description:统计店铺各状态商品数量
     * @author: chenyu.wang
     * @date: 2020-08-19
     */
    @Override
    public ShopGoodsCountVO queryShopGoodsCount(Long shopId) {
        ShopGoodsCountVO vo = new ShopGoodsCountVO();
        vo.setAllGoodsInTheShop(this.getShopAllGoodsCount(shopId));
        vo.setStoresHaveGoods(this.getShopAllStoresHaveGoods(shopId));
        vo.setNoGoodsInTheStore(this.getShopAllNoGoodsInTheStore(shopId));
        return vo;
    }

    /*
     * @description:店铺运营产品统计销量排名
     * @author: chenyu.wang
     * @date: 2020-08-19
     */
    @Override
    public Page<ShopGoodsSalesRankVO> queryShopSalesRanks(Integer pageNo, Integer pageSize, Long shopId) {
        Page<ShopGoodsSalesRankVO> page = new Page<>(pageNo, pageSize);
        List<ShopGoodsSalesRankVO> list = shopGoodsCountMapper.queryShopSalesRanks(page, shopId);
        if (CustomizeUtil.listIsNotNull(list)){
            for (ShopGoodsSalesRankVO vo : list) {
                CommodityInfoDTO commodityInfoDTO = commodityInfoService.selectById(vo.getGoodsId());
                if (commodityInfoDTO!=null){
                    vo.setGoodsName(commodityInfoDTO.getName());
                    vo.setLogoUrl(commodityInfoDTO.getLogoUrl());
                }
            }
        }
        page.setRecords(list);
        return page;
    }

    /**
     * @description:店铺运营产品统计最近上新
     * @author: chenyu.wang
     * @date: 2020-08-19
     */
    @Override
    public Page<ShopGoodsSalesRankVO> queryShopGoodsNew(Integer pageNo, Integer pageSize, Long shopId) {
        Page<ShopGoodsSalesRankVO> page = new Page<>(pageNo, pageSize);
        List<ShopGoodsSalesRankVO> list = shopGoodsCountMapper.queryShopGoodsNew(page, shopId);
        page.setRecords(list);
        return page;
    }

    /**
     * @description:店铺所有产品数量
     * @author: chenyu.wang
     * @date: 2020-08-19
     */
    private Integer getShopAllGoodsCount(Long shopId) {
        Wrapper<CommodityInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", ZERO);
        wrapper.eq("audit_status",ONE);
        wrapper.eq("shop_id", shopId);
        return commodityInfoService.selectCount(wrapper);
    }

    /**
     * @description:店铺所有已上架的商品数量
     * @author: chenyu.wang
     * @date: 2020-08-19
     */
    private Integer getShopAllStoresHaveGoods(Long shopId) {
        Wrapper<CommodityInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", ZERO);
        wrapper.eq("is_publish", ONE);
        wrapper.eq("shop_id", shopId);
        wrapper.eq("audit_status",ONE);
        return commodityInfoService.selectCount(wrapper);
    }

    /**
     * @description:店铺所有未上架的商品数量
     * @author: chenyu.wang
     * @date: 2020-08-19
     */
    private Integer getShopAllNoGoodsInTheStore(Long shopId) {
        Wrapper<CommodityInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", ZERO);
        wrapper.eq("is_publish", ZERO);
        wrapper.eq("shop_id", shopId);
        wrapper.eq("audit_status",ONE);
        return commodityInfoService.selectCount(wrapper);
    }
}
